package cn.jksoft.businessclient.base;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.igexin.sdk.PushConsts;

import java.util.List;

import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.OrderPushEntity;
import cn.jksoft.businessclient.ui.activity.MainActivity;
import cn.jksoft.businessclient.ui.activity.OrderDetailActivity;

/**
 * Created by RogersHuang on 16/4/28.
 */
public class JKPushReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_CLIENTID:
                String cid = bundle.getString("clientid");
                BaseApplication.getInstance().getPreference().putString(BaseConfig.PUSH_CID,cid).commit();
                // TODO:处理cid返回
                break;
            case PushConsts.GET_MSG_DATA:
                byte[] payload = bundle.getByteArray("payload");
                if (payload != null) {
                    String data = new String(payload);
                    Log.i("PushContent",data);
                    // TODO:接收处理透传（payload）数据
                    OrderPushEntity entity = new Gson().fromJson(data, OrderPushEntity.class);
                    Notification.Builder builder = new Notification.Builder(context);
                    Intent mIntent = new Intent(context,OrderDetailActivity.class);
                    mIntent.putExtra("outboundId",entity.getOutboundId());
                    mIntent.putExtra("messageId",entity.getMessId());
                    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                    builder.setContentIntent(pendingIntent);
                    builder.setSmallIcon(R.drawable.push);
                    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon));
                    builder.setAutoCancel(true);
                    builder.setContentTitle(entity.getTitle());
                    builder.setContentText(entity.getContent());
                    //设置点击跳转
                    Intent hangIntent = new Intent();
                    hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    hangIntent.putExtra("outboundId",entity.getOutboundId());
                    hangIntent.setClass(context, MainActivity.class);
                    //如果描述的PendingIntent已经存在，则在产生新的Intent之前会先取消掉当前的
                    PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                    builder.setFullScreenIntent(hangPendingIntent, true);
                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
                    notificationManager.notify(0, builder.build());

                }
                break;
            default:
                break;
        }
    }
    public boolean isAppOnForeground(Context context) {
        // Returns a list of application processes that are running on the
        // device

        ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = context.getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        return false;
    }
}
